package cn.kgc.tangcco.bean;

/**
 * @author 李昊哲
 * @Description 电影
 * @create 2020/8/31 上午10:10
 */
public class Book implements Comparable{
    /**
     * 小说名
     */
    private String name;
    /**
     * 作者
     */
    private String author;
    /**
     * 单价
     */
    private int price;
    /**
     * 销量
     */
    private int sales;

    public Book() {
    }

    public Book(String name, String author, int price, int sales) {
        this.name = name;
        this.author = author;
        this.price = price;
        this.sales = sales;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public int getSales() {
        return sales;
    }

    public void setSales(int sales) {
        this.sales = sales;
    }

    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", author='" + author + '\'' +
                ", price=" + price +
                ", sales=" + sales +
                '}';
    }

    @Override
    public int compareTo(Object o) {
        Book book = (Book) o;
        // 作者的hashCode
        int x = author.hashCode() - book.getAuthor().hashCode();
        // 销量
        int y = book.getSales() - sales;
        // 单价
        int z = book.getPrice() - price;
        if(x != 0){
            // 作者不同的时候按照作者的hashCode升序
            return x;
        } else if(y != 0){
            // 作者相同的时候按照销量降序
            return y;
        } else if(z != 0){
            // 作者相同并且该作者的作品销量的时候按照该作品销量降序
            return z;
        }
        return name.hashCode() - book.getName().hashCode();
    }
}
